home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
-
- /* function prototype */
- char* GetEnv(char *name);
-
- main()
- {
- /* Send the mime type to the server to expect HTML output */
- printf("Content-type: text/html\n\n");
-
- printf("<HTML>\n");
- printf("<HEAD><TITLE>CGI Script How-to: Test Script</TITLE></HEAD>\n");
- printf("<BODY>\n");
-
- printf("<H1>CGI Environment Variables</H1>\n");
-
- printf("<B>SERVER_SOFTWARE</B> = %s<BR>\n", GetEnv("SERVER_SOFTWARE"));
- printf("<B>SERVER_NAME</B> = %s<BR>\n", GetEnv("SERVER_NAME"));
- printf("<B>SERVER_PORT</B> = %s<BR>\n", GetEnv("SERVER_PORT"));
- printf("<B>GATEWAY_INTERFACE</B> = %s<BR>\n", GetEnv("GATEWAY_INTERFACE"));
- printf("<B>SCRIPT_NAME</B> = %s<BR>\n", GetEnv("SCRIPT_NAME"));
- printf("<B>REMOTE_ADDR</B> = %s<BR>\n", GetEnv("REMOTE_ADDR"));
- printf("<B>REMOTE_HOST</B> = %s<BR>\n", GetEnv("REMOTE_HOST"));
- printf("<B>HTTP_USER_AGENT</B> = %s<BR>\n", GetEnv("HTTP_USER_AGENT"));
- printf("<B>HTTP_ACCEPT</B> = %s<BR>\n", GetEnv("HTTP_ACCEPT"));
-
- printf("</BODY></HTML>\n");
- exit(0);
- }
-
-
- /* function GetEnv
- *
- * Gets the named environment variable and returns a printable string.
- *
- * Returns: environment value if defined,
- * otherwise returns an empty string "".
- */
-
- char* GetEnv(char *name)
- {
- char *value = getenv(name);
-
- /* If the environment variable is not defined then getenv would return a NULL
- * or 0, however, if we assume the variable is defined and use this value in
- * a function like strcmp() then the program could abort with a fatal error.
- * Therefore, we don't want to return a NULL string so we check whether getenv
- * returns a NULL string and instead we return an empty string "" if the
- * variable is not defined. Otherwise we return the value returned from getenv
- * like normal. The value returned from GetEnv is therefore safe to use in
- * string comparisons, print statements, and the like.
- */
-
- if (value == NULL) return "";
- else return value;
- }
-
- /*
- * end of test-env.c
- */
-